package user;

import constant.Constant;
import book.Library;
import constant.Constant;
import util.LibrarySingleton;
import util.PermissionException;

public class ProxyUser {
    private User realUser;  //被代理的正真用户
    private Library library;

    public ProxyUser(User user){
        this.realUser=user;  //对被代理的对象进行赋值
        library= LibrarySingleton.getLibrary();
    }
    public User getRealUser(){
        return realUser;
    }
    //其他操作
    //调用菜单
    public int display(){
        return this.realUser.display();
    }
    //--------------------------------管理员相关方法------------------------------//
    private void checkRealUserWhetherAdminUser(String exceptionMessage){
        if(!(realUser instanceof AdminUser)){
            //自定义异常类，表示权限异常
            throw new PermissionException(exceptionMessage);
        }
    }
    //添加书籍操作
    public void addBook(){
//查看代理的对象是不是管理员
     checkRealUserWhetherAdminUser("普通用户没有权限上架图书");
        ((AdminUser)(this.realUser)).addBook();
    }
    //更新书籍操作
    public void updateBook(){

    }
    //删除书籍
    public void removeBook(){

    }
    //统计每本书的借阅次数
    public void borrowCount(){

    }
    //查询最受欢迎的前n本书
    public void generateBook(){

    }

    //查看库存状态
    public void checkInventoryStatus() {

    }

    //并移除上架超过一年的图书
    public void checkAndRemoveOldBooks() {

    }
    //--------------------------------普通用户相关方法------------------------------//
    //借阅图书
    public void borrowBook(){

    }
    //归还图书
    public void returnBook(){

    }
    //查看个人借阅情况
    public void viewBorrowBooks(){

    }

    public void handleOperation(int choice) {

        if(this.realUser instanceof AdminUser) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                default:
                    break;
            }
        }else {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowBooks();
                    break;
            }
        }

    }


}



